/*
 * Created on Jun 1, 2009
 */
package com.mgmaps.platform;

import javax.microedition.media.Control;
import com.mgmaps.utils.ExtLogger;
import com.nokia.mid.ui.DeviceControl;

public class PlatformNokia implements IPlatformHandler {
	
	private BacklightThread lightThread;
	
  /**
   * Vibrate on Nokia.
   * @param length length in milliseconds
   * @see com.mgmaps.platform.trapster.platform.IPlatformHandler#vibrate(int)
   */
  public void vibrate(int length) {
    DeviceControl.startVibra(100, length);
    // Display.getDisplay(Trapster.instance).vibrate(length);
  }

  /**
   * Not used.
   * @param controls
   * @param device
   * @return false
   * @see com.mgmaps.platform.trapster.platform.IPlatformHandler#setAudioPath(javax.microedition.media.Control[], int)
   */
  public boolean setAudioPath(Control[] controls, int device) {
    return false;
  }

  /**
   * Set the volume using device-dependent code.
   * @param vol new volume (0-100)
   * @see com.mgmaps.platform.IPlatformHandler#setVolume(int)
   */
  public void setVolume(int vol) {
    // nothing to do, VolumeControl setting should be enough
  }

  /**
   * Get device id.
   * @return device id (IMEI), null if using default imei detection method
   * @see com.mgmaps.platform.IPlatformHandler#getDeviceID()
   */
	public String getDeviceID() {
	  return null;
	}

	/**
	 * Get device name.
	 * 
	 * @return device name ("Nokia xxx, platform, ..."), null if using default detection method
	 * @see com.mgmaps.platform.IPlatformHandler#getDeviceName()
	 */
	public String getDeviceName() {
	  return null;
	}
	
	/**
	 * Get user agent.
	 * 
	 * @return null, we're not using it for Nokia for now
	 * @see com.mgmaps.platform.IPlatformHandler#getUserAgent()
	 */
	public String getUserAgent() {
	  return null;
	}

  public String getCarrierName() {
    return "";
  }
  
  public String getPhoneNumber() {
    return "";
  }

	/**
	 * Turn backlight off.
	 * 
	 * @see com.mgmaps.platform.IPlatformHandler#backlightOff()
	 */
	public void backlightOff() {
		if(lightThread != null)
			lightThread.stop();
	}

	/**
	 * Turn backlight on.
	 * 
	 * @see com.mgmaps.platform.trapster.platform.IPlatformHandler#backlightOn()
	 */
	public void backlightOn() {
		 lightThread = new BacklightThread(); 
		 lightThread.start();
	}

	/*
	private String getSwVersion() {
		String sw = "";
		String string = "";
		int index = 0;
		char c = 'a';
		while (true) {
			c = platformString.charAt(index);
			if (c != '/')
				index++;
			else
				break;
		}
		index++;
		c = 'a';
		int len = platformString.length();
		while (c != '/') {
			c = platformString.charAt(index);
			string += c;
			if (index < len)
				index++;
			if (index == len) {
				sw = string;
				break;
			}
		}
		if (sw.equals(""))
			sw = string.substring(0, string.length() - 1);
		return sw;
	}

	private String getPlatform() {
		String platform = "";
		String sw_platform = "sw_platform";
		String test = "";
		String s60 = "";
		int sw_length = sw_platform.length();
		int index = 0;
    int len = platformString.length();
		while (platform.equals("")) {
			test = platformString.substring(index, index + sw_length);
			if (test.equalsIgnoreCase(sw_platform)) {
				index = index + sw_length + 1;
				s60 = platformString.substring(index, index + 3);
				if (s60.equalsIgnoreCase("s60"))
					platform = s60;
				else
					platform = "N/A";
			}
			if (index < (len - sw_length))
				index++;
			else
				platform = "N/A";
		}
		return platform;
	}

	private String getPlatformVersion() {
		String platformVersion = "";
		int index = platformString.length() - 1;
		char c = 'a';
		char temp_c = 'a';
		while (platformVersion.equals("")) {
			c = platformString.charAt(index);
			if (c == ';') {
				String temp = "";
				int temp_i = index - 1;
				while (temp.equals("")) {
					temp_c = platformString.charAt(temp_i);
					if (temp_c == '=')
						temp = platformString.substring(temp_i + 1, index);
					if (temp_i > 0)
						temp_i--;
				}
				platformVersion = temp;
			}
			if (index > 0)
				index--;
			else
				platformVersion = "N/A";
		}
		return platformVersion;
	}
	*/
	

	/**
   * Backlight thread.
   */
  class BacklightThread extends Thread {

    private boolean running = true;
    private Object lock = new Object();

    public void stop() {
      synchronized (lock) {
        running = false;
        lock.notifyAll();
      }

    }

    /**
     * Repeatedly call DeviceControl.setLights
     * 
     * @see java.lang.Thread#run()
     */
    public void run() {
      while (running) {
        DeviceControl.setLights(0, 100);

        synchronized (lock) {
          try {
            lock.wait(5000);
          }
          catch (InterruptedException e) {
            ExtLogger.handle(e, false, "Exception in Nokia backlight on thread");
          }
        }
      }
    }

  }

  /**
   * Get extension string.
   * @return HTTP extension string index = 0
   * @see com.mgmaps.platform.IPlatformHandler#getBlackberryHTTPExtension()
   */
  public String getBlackberryHTTPExtension() {
    return "";
  }

  /**
   * Open a website in the default browser. Not implemented.
   * @param url URL to open
   */
  public void openWebsite(String url) {
  }
}
